package com.snow.useviewdemo.html;

import android.annotation.SuppressLint;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;

import com.snow.useviewdemo.R;
import com.snow.useviewdemo.base.BaseActivity;

import butterknife.BindView;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019-09-09
 * desc   :通过webview加载h5含视频
 */
public class WebViewH5VideoAct extends BaseActivity {
    private String h5Url = "https://preview.baike.qq.com/mobile/article.html?docid=tx00097008hmkttq&tmenulogo=hide";

    @BindView(R.id.tbs_webview)
    TBSWebView tbsWebview;
    //当前屏幕是否是横屏
    private boolean isScreenLandscape = false;

    @Override
    protected int getLayoutId() {
        return R.layout.act_html_webview_video;
    }

    @Override
    public void onConfigurationChanged(Configuration config) {
        super.onConfigurationChanged(config);
        switch (config.orientation) {
            case Configuration.ORIENTATION_LANDSCAPE:
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                isScreenLandscape = true;
                break;
            case Configuration.ORIENTATION_PORTRAIT:
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
                isScreenLandscape = false;
                break;
        }
    }

    @SuppressLint("WrongConstant")
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isScreenLandscape) {
                if (tbsWebview != null) {
                    tbsWebview.hideCustomView();
                    return true;
                }
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void setViewData(Bundle savedInstanceState) {
        tbsWebview.getSettings().setJavaScriptEnabled(true);

        tbsWebview.loadUrl(h5Url);
    }


}
